package com.chzc.fm.duotin.ui.other;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.utils.NetWorkUtils;
import com.chzc.fm.duotin.widget.TitleBar;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ForgetPwdActivity extends BaseActivity {

    private EditText editEmail;
    private TextView textTip;
    private Button btnFind;
    private TitleBar titleBar;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    protected void initViews() {
        editEmail = (EditText) findViewById(R.id.et_input_email);
        textTip = (TextView) findViewById(R.id.tv_show_tip);
        btnFind = (Button) findViewById(R.id.btn_find_pwd);
        titleBar = (TitleBar) findViewById(R.id.title_bar);
    }

    @Override
    protected void initEvents() {
        editEmail.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                textTip.setVisibility(View.INVISIBLE);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        btnFind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = editEmail.getText().toString();
                if(mNetWorkUtils.getConnectState() == NetWorkUtils.NetWorkState.NONE){
                    textTip.setText("网络错误请检查您的网络环境");
                    textTip.setVisibility(View.VISIBLE);
                }else if(!(email.length() > 0)){
                    textTip.setText("请输入邮箱");
                    textTip.setVisibility(View.VISIBLE);
                }else if(!isEmail(email)){
                    textTip.setText("邮箱格式不正确");
                    textTip.setVisibility(View.VISIBLE);
                }
            }
        });
        titleBar.setLeftListener(new TitleBar.mTitleBarListener() {
            @Override
            public void onClick(View v) {
                ForgetPwdActivity.this.finish();
            }
        });
    }

    /**
     * 判断邮箱格式
     * @param strEmail
     * @return
     */
    public static boolean isEmail(String strEmail) {
        String strPattern = "^[a-zA-Z][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";
        Pattern p = Pattern.compile(strPattern);
        Matcher m = p.matcher(strEmail);
        return m.matches();
    }

    @Override
    protected void init() {
        titleBar.setRight(false);
        titleBar.setLeft(true, R.drawable.arrow_back_gray);
        titleBar.setTitleBarVisibility(true, "忘记密码");
    }
}
